#include "wificontroller.h"
#include <QDebug>
#include <QSettings>

const static int MAX_SIGNAL = 120;

WifiController::WifiController(QObject *parent) : QObject(parent)
{
    m_process = new QProcess(this);
    m_curAction = readAllInfo;
    connect(m_process,SIGNAL(finished(int,QProcess::ExitStatus)),
            this,SLOT(on_processFinished(int,QProcess::ExitStatus)));
    isConnected = false;
    m_curESSID = "";
    isConnectting = false;

    //10秒钟过后连接超时
    m_timeOutTimer = new QTimer(this);
    connect(m_timeOutTimer,SIGNAL(timeout()),
            this,SLOT(on_timeOut()));

    //每隔3秒钟刷新一次wifi信息
    m_wifiInfoLoopTimer = new QTimer(this);
    m_wifiInfoLoopTimer->start(3000);
    connect(m_wifiInfoLoopTimer,SIGNAL(timeout()),this,SLOT(on_needLoopWifiInfo()));
}

void WifiController::on_processFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
    Q_UNUSED(exitCode);
    Q_UNUSED(exitStatus);
    m_process->kill();
    //读取系统命令返回字符串
    QString res = m_process->readAllStandardOutput();
    switch (m_curAction) {
    //读取当前wifi信息
    case readAllInfo:
    {
        QStringList wifiInfoList;
        QStringList list = res.split("         Cell");
        for(int i=1;i<list.size();i++){
            QString name   = list.value(i).split("ESSID:\"").value(1).split("\"").value(0);
            QString quality = list.value(i).split("Quality=").value(1).split(" ").value(0);
            int signal = ((quality.split("/").value(0).toInt()-1)*4/quality.split("/").value(1).toInt())+1;
            bool isE    = list.value(i).split("Encryption key:").value(1).split(" ").value(0).contains("on");
            QString enc = isE ? "1" : "0";
            QString con = isConnected ? "1" : "0";
            if(isConnectting)
            {
                con = "2";
            }
            wifiInfoList<<name+"|"+QString::number(signal)+"|"+enc+"|"+m_curESSID+"|"+con;
        }
        if(wifiInfoList.size()!=0)
        {
            emit refreshWifiInfo(wifiInfoList);
        }
    }
        break;
    case connectOne:
    {
        if(res.contains("ssid"))
        {
            isConnected = true;
            isConnectting =false;

            emit hasConnected();
        }else
        {
            isConnected = false;
        }
    }
        break;
    default:
        break;
    }
}

void WifiController::get_WifiInfo()
{
    m_curAction = readAllInfo;
    m_process->start("iwlist wlan0 scan");
    m_process->waitForStarted();
    m_process->waitForFinished();
}

void WifiController::connect_Wifi(QString ESSID, QString psw)
{
    m_curESSID = ESSID;
    qDebug()<<"connect_Wifi"<<m_curESSID;
    //密码写入配置文件
    QSettings settings("wifi.ini",QSettings::IniFormat);
    settings.setValue(ESSID,psw);

    //连接wifi
    connect_Wifi(ESSID);
}

void WifiController::connect_Wifi(QString ESSID)
{
    m_curESSID = ESSID;
    qDebug()<<"connect_Wifi"<<m_curESSID;
    //置读取标注位
    m_curAction = connectOne;

    //连接wifi
    QSettings settings("wifi.ini",QSettings::IniFormat);
    QString psw =settings.value(ESSID).toString();
    qDebug()<<(QString("wpa_passphrase %1 %2 > /etc/wpa_supplicant/wpa_supplicant.conf").arg(ESSID).arg(psw).toLatin1());
    system(QString("wpa_passphrase %1 %2 > /etc/wpa_supplicant/wpa_supplicant.conf").arg(ESSID).arg(psw).toLatin1());
    system("ifdown wlan0");
    qDebug()<<("ifdown wlan0");
    system("ifup wlan0");
    qDebug()<<("ifup wlan0");
    m_process->waitForStarted();
    m_process->waitForFinished();


    isConnectting = true;
    m_timeOutTimer->start(7000);
}

void WifiController::forget_Wifi(QString ESSID)
{
    QSettings settings("wifi.ini",QSettings::IniFormat);
    settings.setValue(ESSID,"");
    disconnect_Wifi();
    m_curESSID = "";
}

void WifiController::disconnect_Wifi()
{
    m_process->start("echo "" > /etc/wpa_supplicant/wpa_supplicant.conf");
    m_process->waitForStarted();
    m_process->waitForFinished();
    m_process->start("ifdown wlan0");
    m_process->waitForStarted();
    m_process->waitForFinished();
    m_process->start("ifup wlan0");
    m_process->waitForStarted();
    m_process->waitForFinished();
    m_curESSID = "";
}

bool WifiController::has_Password(QString ESSID)
{
    QSettings settings("wifi.ini",QSettings::IniFormat);
    QString psw =settings.value(ESSID).toString();
    return psw.isEmpty() ? false : true;
}


void WifiController::on_timeOut()
{
    isConnectting = false;
    m_timeOutTimer->stop();
}

void WifiController::on_needLoopWifiInfo()
{
    m_curAction = connectOne;
    m_process->start("wpa_cli status");
    m_process->waitForStarted();
    m_process->waitForFinished();
    m_curAction = readAllInfo;
    m_process->start("iwlist wlan0 scan");
    m_process->waitForStarted();
    m_process->waitForFinished();
}
